package com.iyunwen.datatool.datastorage;


import com.iyunwen.datatool.common.annotations.EventTypeAnnotation;
import com.iyunwen.datatool.dataparse.DataParse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 数据加载策略控制器
 * @author: wangyu
 * @create: 2020-03-20 14:52
 * @version:
 */
@Component
public class DataStorageContext {
    private final Map<Integer, DataStorage> dataStorageMap = new ConcurrentHashMap<>();

    @Autowired
    public void strategyInterface(Map<String, DataStorage> strategyMap) {
        for (Map.Entry<String, DataStorage> entry : strategyMap.entrySet()) {
            DataStorage value = entry.getValue();
            EventTypeAnnotation declaredAnnotation = value.getClass().getDeclaredAnnotation(EventTypeAnnotation.class);
            dataStorageMap.put(declaredAnnotation.eventType().getIndex(), value);
        }
    }

    /**
     * 策略选择
     *
     * @param eventType
     * @return
     */
    public DataStorage getDataStorage(int eventType) {
        if (dataStorageMap.containsKey(eventType)) {
            return dataStorageMap.get(eventType);
        }
        return null;
    }
}
